package com.zhanghu.backendZray.valid;

import com.zhanghu.backendZray.pojo.request.dailyPlanTask.DailyPlanRequest;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class TaskTypeValidator implements ConstraintValidator<ValidTaskType, DailyPlanRequest> {

    @Override
    public void initialize(ValidTaskType constraintAnnotation) {
    }

    @Override
    public boolean isValid(DailyPlanRequest dailyPlanRequest, ConstraintValidatorContext context) {
//        if (yourClass.getTaskType() == 0 && yourClass.getRememberStartDate() == null) {
//            return false;
//        }
//        if (yourClass.getTaskType() == 1 && yourClass.getScheduledTaskDateTime() == null) {
//            return false;
//        }
//        return true;

        if (dailyPlanRequest == null) {
            return true; // 空对象不做校验
        }
        if (dailyPlanRequest.getTaskType() == null) {
            return true; // taskType 为空不做校验
        }
        boolean isValid = true;
        if (dailyPlanRequest.getTaskType() == 0) {
            if (dailyPlanRequest.getRememberStartDate() == null) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("记忆开始时间不能为空")
                        .addPropertyNode("rememberStartDate")
                        .addConstraintViolation();
                isValid = false;
            }
        } else if (dailyPlanRequest.getTaskType() == 1) {
            if (dailyPlanRequest.getScheduledTaskDateTime() == null) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("定时任务时间不能为空")
                        .addPropertyNode("scheduledTaskDateTime")
                        .addConstraintViolation();
                isValid = false;
            }
        }
        return isValid;
    }
}